var e = require("yb_shop/utils/core.js");
App({
  onShow: function (e) {
    console.log("APP onShow !!!!!!");
    console.log(e);
    var page = getCurrentPages();
    console.log(page);
  },
  version: '1.1.3',
  getExtC: function (obj) {
    typeof obj == "function" && obj();
    return;
    var that = this;
    if (wx.getExtConfig) {
      wx.getExtConfig({
        success: function (res) {
          console.log(res)
          if (res.errMsg == "getExtConfig: ok") {
            that.globalData.appid = res.extConfig.app_key ? res.extConfig.app_key : '';
            that.globalData.app_name = res.extConfig.app_name ? res.extConfig.app_name : getApp().globalData.app_name;
            that.siteInfo.siteroot = res.extConfig.siteroot ? res.extConfig.siteroot : '';
            that.siteInfo.uniacid = res.extConfig.uniacid ? res.extConfig.uniacid : '';

            typeof obj == "function" && obj()
          } else {
            e.error('app.js:' + res.errMsg)
          }
        }
      })
    }
  },
  onLaunch: function (e) {
    var that=this;
    console.log(e);
    var page = getCurrentPages();
    console.log(page);
    var site_info = require("siteinfo.js");
    var pid=0;
    var that = this;
    that.siteInfo = site_info;

    if (e.query.pid){
      pid=e.query.pid
    }
    console.log(pid)
    that.setCache('pid',pid);
     that.getExtC(function () {
      that.get_menu(function (t) {
       // console.log(t)
      });
    })
  },

  //分销发展下线
  set_share_pid:function(pid){
   var uid =this.getCache("userinfo").uid;
   if(uid && pid && pid!=0){
     e.get('Distribe/addman', {
       uid:uid,
       pid:pid
    }, function (t) {
         console.log(t)
       })
   }
  },

  get_poster:function(k){
    var that = this;
    var data = e.pdata(k);
      ptype = data.type,
      url = data.url ? data.url : '',
      id = data.id ? data.id : '',
      img = data.img ? data.img : '',
      price = data.price ? data.price : '',
      original_price = data.original_price ? data.original_price : '';
  },

  requirejs: function (e) {
    var js = "yb_shop/utils/" + e + ".js";
    return require(js);
  },
  get_menu: function (n) {

    var that = this;
    var cache = that.getCache('menu');
    if (cache) {
      "function" == typeof n, n(2);
    } else {
      wx.request({
        url: that.siteInfo.siteroot + '?i=' + that.siteInfo.uniacid + '&t=undefined&v='+that.version+'&from=wxapp&c=entry&a=wxapp&do=index_tabbar&m=yb_shop&sign=1d917db727d0aa4e23ca117826fa3153', //仅为示例，并非真实的接口地址
        data: {},
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (t) {
          if (t.errMsg == "request:ok") {

            if (typeof (t.data) == "string") {
              if (t.data.indexOf("html") >= 0 && t.data.indexOf("head") >= 0 && t.data.indexOf("body") >= 0) {
                e.error('请求错误003');
                return;
              }
            }

            if (t.data == '') {
              console.log('请求异常！');
              return;
            }
          
            //转json对象
            if (typeof t.data =='string'){
              t.data = e.json_parse(t.data);
            }

            if (t.data.code == 0) {
              var info = t.data.info;
              if (info.page != null && info.page != undefined) {
                that.tabBar = info.tabbar;
                that.page = info.page;
                var iii = 0;
                for (var iii = 0; iii < that.tabBar.list.length; iii++) {
                  var bbb = that.tabBar.list[iii];
                  if (bbb.url.indexOf("?") < 0) {
                    bbb.url += "?tabbar_index=" + iii;
                  }
                  else {
                    bbb.url += "&tabbar_index=" + iii;
                  }
                }  
                that.setCache("menu", info.tabbar, 5);
              }
              else {
                e.alert(info.msg);
                return;
              }
              
            }
            "function" == typeof n, n(1)
          } else {
            e.alert(t.errMsg)
          }
        }
      })
    }
  },
  getUserInfo: function (userInfo, t, encryptedData=null,iv=null) {

    console.log("getUserInfo !!!!!!");

    var n = this,
      openid = '',
      sessionKey = '',
      a = n.getCache("userinfo");
    if (a) return void (t && "function" == typeof t && t(a));
    wx.login({
      success: function (o) {
        if (!o.code) return void e.alert("获取用户登录态失败2");
        console.log(o)
        e.get("user/openid", {
          wx_code: o.code,
          iv:iv,
          encryptedData: encryptedData
        }, function (o) {
          console.log(o)
          if (o.code == 0) {

            if (!o.info.errcode) {
              openid = o.info.openid;
              sessionKey = o.info.session_key;
            } else {
              e.alert('errcode:' + o.info.errcode + ';errmsg:' + o.info.errmsg);
              return false;
            }
          } else {
            e.alert(o.msg);
            "function" == typeof t && t(1000);
            return false;
          }
            
          e.get("user/Login", {
            wx_openid: openid,
            wx_unionid: o.info.unionid ? o.info.unionid : null,
            nick_name: userInfo.nickName,
            user_headimg: userInfo.avatarUrl,
            pid:n.getCache('pid')
          }, function (e) {
            console.log(e)
            //this.globalData.uid=e.info
            userInfo.openid = openid, userInfo.uid = e.info, n.setCache("userinfo", userInfo), t && "function" == typeof t && t(a)
          })
        })
      },
      fail: function () {
        "function" == typeof t && t(1000)
        e.alert("获取用户信息失败")
      }
    })
  },
  get_user_name:function(uid,that){
 
      e.get("user/UserInfo", {
        uid: uid
      }, function (r) {
        console.log(r)
        that.setData({
          p_name: r.info.nick_name
        })
      })
  },
  check_is_tabbar: function(){
    var that = this;
    var page = getCurrentPages()[0];
    console.log(page);

    if (this.siteInfo.native_tabbar) {
      page.setData({
        tabbar_index: -1,
        showtabbar: false
      })
      return;
    }

    if (page.options.tabbar_index)
    {
      page.setData({
        tabbar_index: page.options.tabbar_index,
      })
    }
    else
    {
      var value = page.route;
      for (var i = 0; i < that.tabBar.list.length; i++) {
        var url = that.tabBar.list[i].url;
        if (url.indexOf(value) >= 0) {
          page.setData({
            tabbar_index: i,
          })
          break;
        }
      }
    }

    if (page.data.tabbar_index >= 0) {
      page.setData({
        showtabbar: that.page.show_tabbar == 'true' ? true : false,
      })
    }
  },
  
  isInArray: function (arr, value) {
    for (var i = 0; i < arr.length; i++) {
      if (value == arr[i].key) {
        var str=arr[i].url;
        return str.charAt(str.length - 1);
      }
    }
    return false;
  },

  page:{
    name: '小程序',
    nv_color: '#ffffff',
    bg_color: '#f2f2f2',
    text_color: '#000000',
    bg_img: '',
    open_img: {},
    show_tabbar: 'true',
    login_img:'/yb_shop/static/images/login.png'
  },

  tabBar: {
    bg_color: "#ffffff",
    text_color: "#333333",
    select_color: "#fe4b71",
    list: [
      {
        imgurl: "/yb_shop/pages/index/index",
        name: "首页",
        key: 'index',
        page_icon: "/yb_shop/static/icon/gray_home.png",
        page_select_icon: "/yb_shop/static/icon/red_home.png"
      },
      {
        imgurl: "/yb_shop/pages/shop_coupon/index",
        name: "优惠券",
        key: 'shop_coupon',
        page_icon: "/yb_shop/static/icon/gray_find.png",
        page_select_icon: "/yb_shop/static/icon/red_find.png"
      },
      {
        imgurl: "/yb_shop/pages/product/index",
        name: "商品",
        key: 'product',
        page_icon: "/yb_shop/static/icon/gray_cate.png",
        page_select_icon: "/yb_shop/static/icon/red_cate.png"
      },
      {
        imgurl: "/yb_shop/pages/member/cart/index",
        name: "购物车",
        key: 'cart',
        page_icon: "/yb_shop/static/icon/gray_cart.png",
        page_select_icon: "/yb_shop/static/icon/red_cart.png"
      },
      {
        imgurl: "/yb_shop/pages/member/index/index",
        name: "会员中心",
        key: "member_index",
        page_icon: "/yb_shop/static/icon/gray_people.png",
        page_select_icon: "/yb_shop/static/icon/red_people.png"
      }
    ]
  },
  /*
    *获取缓存
     * @param e 缓存名称
     * @param t 缓存值
  */
  getCache: function (e, t) {
    var i = +new Date / 1000,
      n = "";
    i = parseInt(i);
    try {
      n = wx.getStorageSync(e + this.globalData.appid), n.expire > i || 0 == n.expire ? n = n.value : (n = "", this.removeCache(e))
    } catch (e) {
      n = void 0 === t ? "" : t
    }
    return n = n || ""
  },
  /*
   *创建缓存
    * @param e 缓存名称
    * @param t 缓存值
    * @param i 缓存时间
 */
  setCache: function (e, t, i) {
    var n = +new Date / 1000,
      a = true,
      o = {
        expire: i ? n + parseInt(i) : 0,
        value: t
      };
    try {
      wx.setStorageSync(e + this.globalData.appid, o)
    } catch (e) {
      a = false
    }
    return a
  },
  /*
    *清除缓存
     * @param e 缓存名称
  */
  removeCache: function (e) {
    var t = true;
    try {
      wx.removeStorageSync(e + this.globalData.appid)
    } catch (e) {
      t = false
    }
    return t
  },
  ab: function (e) { },
  util: require("./we7/resource/js/util.js"),
  siteInfo: require("siteinfo.js"),
  globalData: {
    appid: 'liu2417301781',
    userInfo: null,
    app_name: '壹佰小程序'
  },
  config: {

    background: '#8b8b8b',//首页搜索、个人汇中心背景#000cf.color.background
    button_color: '#fff',//商品详情按钮，提交订单、确认支付按钮，购物车结算按钮
    alert_color: '#fff',//信息提示，表单提交按钮oI9vSwl9OO4Vggsuy65nL_qZYprY
    font_color: 'black',
    selectedColor: "#df2f20",

    kuan:'款',
    dan:'单',
    fu:'付',
    gou:'购',
    huo:'货'
  },
  //
  //拼团
  redirect: function (url, param) {
    wx.navigateTo({
      url: '/yb_shop/pages/pintuan/pages/' + url + '?' + param
    })
  },
  showModal: function (that) {
    var animation = wx.createAnimation({
      duration: 200,
    })
    animation.opacity(0).rotateX(-100).step();
    that.setData({
      // animationData:animation.width('291px'),
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      that.setData({
        animationData: animation
      });
    }.bind(that), 200)
  },
  showToast: function (that, title) {
    var toast = {};
    toast.toastTitle = title;
    that.setData({
      toast: toast
    })
    var animation = wx.createAnimation({
      duration: 100
    })
    animation.opacity(0).rotateY(-100).step();
    toast.toastStatus = true;
    toast.toastAnimationData = animation.export()
    that.setData({
      toast: toast
    })
    setTimeout(function () {
      animation.opacity(1).rotateY(0).step();
      toast.toastAnimationData = animation
      that.setData({
        toast: toast
      });
    }.bind(that), 100)
    // 定时器关闭 
    setTimeout(function () {
      toast.toastStatus = false
      that.setData({
        toast: toast
      });
    }.bind(that), 2000);
  }})